package com.banksoft.cps.controller.company;

import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.SupplierCompany;
import com.banksoft.cps.bean.query.SupplierCompanyQuery;
import com.banksoft.cps.bean.warpper.SupplierCompanyWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.SupplierCompanyService;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.UserContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/1/12.
 */
@Controller
@RequestMapping("/company/supplierCompany/")
public class CSupplierCompanyController extends ControllerConstants {
    @Autowired
    SupplierCompanyService supplierCompanyService;
    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(SupplierCompanyQuery query){
          Company company = (Company) UserContext.getUser();
        query.setCompanyId(company.getId());
        List<SupplierCompany> list =  supplierCompanyService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(SupplierCompanyQuery query){
          Company company = (Company) UserContext.getUser();
        query.setCompanyId(company.getId());
        long count =  supplierCompanyService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }

    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        SupplierCompanyWarpper bean = supplierCompanyService.findWarpperById(id);
        return toJSONString(createDataTip(bean));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(SupplierCompany bean, HttpServletRequest request){
        try {
              Company company = (Company) UserContext.getUser();
            if(StringUtils.isBlank(bean.getCompanyId())){
                bean.setCompanyId(company.getId());
            }
            supplierCompanyService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "listAll",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String listAll(HttpServletRequest request){
          Company company = (Company) UserContext.getUser();
        SupplierCompanyQuery query = new SupplierCompanyQuery();
        query.setOrderBy("f_name");
        query.setCompanyId(company.getId());
        List<SupplierCompany> deviceList = supplierCompanyService.list(query);
        return toJSONString(createDataTip(deviceList));
    }
}
